package com.tenbent.product.center.example.stu.action;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.tenbent.product.base.action.BaseController;
import com.tenbent.product.base.constants.WebConstans;
import com.tenbent.product.base.exception.TenbentApiNotFoundException;
import com.tenbent.product.center.example.stu.dto.StudentDto;
import io.swagger.annotations.*;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import com.tenbent.product.base.query.page.PageResult;
import com.tenbent.product.base.query.page.Pageable;
import com.tenbent.product.base.web.Page;
import com.tenbent.product.center.example.stu.bo.Student;
import com.tenbent.product.center.example.stu.service.StudentService;

import javax.validation.Valid;

/**
 * API 控制器
 *
 * @author Randy
 *
 *         Created by ThinkPad on 2017/8/3.
 */
@RestController
@Api(description = "学生信息相关接口文档")
public class StudentApiController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(StudentApiController.class);

	public static class Url {

		public static final String save = "/api/eg/student/save";
		public static final String update = "/api/eg/student/update";
		public static final String delete = "/api/eg/student/delete";
		public static final String deletes = "/api/eg/student/deletes";
		public static final String query = "/api/eg/student/query/{id}";

		public static final String queryList = "/api/eg/student/query_list";
		public static final String queryList2 = "/api/eg/student/query_list2";
		public static final String queryPageObj = "/api/eg/student/query_page_obj";
		public static final String queryPageObjMap = "/api/eg/student/query_page_obj_map";
		public static final String queryPageMap = "/api/eg/student/query_page_map";
		public static final String queryPageMapToMap = "/api/eg/student/query_page_map_map";

		public static final String queryMsgI18n = "/api/eg/student/query_msg_i18n";

	}

	@Autowired
	@Qualifier("studentServiceImpl")
	private StudentService studentService;

	@RequestMapping(value = Url.save, method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseStatus(HttpStatus.CREATED)
	@ApiOperation(value = "添加学生信息", notes = "添加学生信息")
	public StudentDto saveApi(@Valid @RequestBody StudentDto stuDto, Errors errors,
			@ApiParam(name = "managerId", value = "操作人ID", required = true) @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		processErrors(errors);
		stuDto.setCreateId(managerId);
		stuDto.setModifyId(managerId);
		Student stu = stuDto.transToBo();
		studentService.add(stu);
		return new StudentDto(stu);
	}

	@RequestMapping(value = Url.update, method = {
			RequestMethod.PUT }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiOperation(value = "修改学生信息", notes = "修改学生信息")
	public StudentDto updateApi(@Valid @RequestBody StudentDto stuDto, Errors errors,
			@ApiParam(name = "managerId", value = "操作人ID", required = true) @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		if (stuDto.getId() == null) {
			errors.rejectValue("id", "student.id.not.empty", getMessage("student.id.not.empty", null));
		}
		processErrors(errors);
		stuDto.setModifyId(managerId);
		Student stu = stuDto.transToBo();
		studentService.update(stu);
		return new StudentDto(stu);
	}

	@RequestMapping(value = Url.delete, method = {
			RequestMethod.DELETE }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiOperation(value = "删除学生信息(单个删除)", notes = "删除学生信息(单个删除)")
	public void deleteApi(@ApiParam(name = "id", value = "学生主键ID", required = true) Integer id,
			@ApiParam(name = "managerId", value = "操作人ID", required = true) @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {
		studentService.deleteLogic(id, managerId);
	}

	@RequestMapping(value = Url.deletes, method = {
			RequestMethod.DELETE }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiOperation(value = "删除学生信息(多个删除)", notes = "删除学生信息(多个删除)")
	public void deletesApi(@ApiParam(name = "ids", value = "学生主键ID数组", required = true) Integer[] ids,
			@ApiParam(name = "managerId", value = "操作人ID", required = true) @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {
		studentService.deleteByIds(ids, managerId);
	}

	@RequestMapping(value = Url.query, method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiOperation(value = "查询学生信息", notes = "根据主键查询学生信息")
	public StudentDto queryApi(@ApiParam(name = "id", value = "学生ID", required = true) @PathVariable Integer id) {

		Student stu = studentService.getByKey(id);
		if (stu == null) {
			throw new TenbentApiNotFoundException("ID ：" + id + " 学生不存在！");
		}
		return new StudentDto(stu);
	}

	@RequestMapping(value = Url.queryList, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiOperation(value = "查询学生信息", notes = "根据姓名，年龄查询学生信息")
	public List<StudentDto> queryApiList(
			@ApiParam(name = "name", value = "学生名称", required = true) @RequestParam(name = "name") String name,
			@ApiParam(name = "age", value = "学生年龄", required = true) @RequestParam(name = "age") Integer age) {

		Student stu = new Student();
		stu.setName(name);
		stu.setAge(age);
		List<Student> list = studentService.queryObj(stu);

		Map<String, Object> paramMap = new HashedMap();
		paramMap.put("name", name);
		paramMap.put("age", age);
		List<Student> list2 = studentService.queryMap(paramMap);

		list.addAll(list2);
		return change.changesToDTOList(list, StudentDto.class);
	}

	@RequestMapping(value = Url.queryList2, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<Map<String, Object>> queryApiList2(@RequestParam(name = "name") String name,
			@RequestParam(name = "age") Integer age) {

		Student stu = new Student();
		stu.setName(name);
		stu.setAge(age);
		List<Map<String, Object>> list = studentService.queryObjToMap(stu);

		Map<String, Object> paramMap = new HashedMap();
		paramMap.put("name", name);
		paramMap.put("age", age);
		List<Map<String, Object>> list2 = studentService.queryMapToMap(paramMap);

		list.addAll(list2);
		return list;
	}

	@RequestMapping(value = Url.queryPageObj, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<StudentDto> queryPageObj(@RequestParam(name = "age", defaultValue = "30") Integer age,
			@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {

		Student stu = new Student();
		stu.setAge(age);
		Pageable pageable = new Pageable(pageNum, pageSize);
		pageable.setParamBizObject(stu);
		PageResult<Student> pageResult = studentService.queryPageObj(pageable);
		return new Page(change.changesToDTOPage(pageResult, StudentDto.class));
	}

	@RequestMapping(value = Url.queryPageObjMap, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<Map<String, Object>> queryPageObjMap(@RequestParam(name = "age", defaultValue = "30") Integer age,
			@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {

		Student stu = new Student();
		stu.setAge(age);
		Pageable pageable = new Pageable(pageNum, pageSize);
		pageable.setParamBizObject(stu);
		PageResult<Map<String, Object>> pageResult = studentService.queryPageObjToMap(pageable);
		return new Page(pageResult);
	}

	@RequestMapping(value = Url.queryPageMap, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<StudentDto> queryPageMap(@RequestParam(name = "age", defaultValue = "30") Integer age,
			@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {

		Map<String, Object> paramMap = new HashedMap();
		paramMap.put("age", age);
		Pageable pageable = new Pageable(pageNum, pageSize);
		pageable.setParamMap(paramMap);
		PageResult<Student> pageResult = studentService.queryPageMap(pageable);
		return new Page(change.changesToDTOPage(pageResult, StudentDto.class));
	}

	@RequestMapping(value = Url.queryPageMapToMap, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<Map<String, Object>> queryPageMapToMap(@RequestParam(name = "age", defaultValue = "30") Integer age,
			@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {

		Map<String, Object> paramMap = new HashedMap();
		paramMap.put("age", age);
		Pageable pageable = new Pageable(pageNum, pageSize);
		pageable.setParamMap(paramMap);
		PageResult<Map<String, Object>> pageResult = studentService.queryPageMapToMap(pageable);
		return new Page(pageResult);
	}

	@RequestMapping(value = Url.queryMsgI18n, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String queryI18N() {

		Object[] args = { 10, 15 };
		String name1 = messageSource.getMessage("name", args, Locale.US);
		String msg1 = messageSource.getMessage("description", args, Locale.US);
		String name2 = messageSource.getMessage("name", args, Locale.CHINA);
		String msg2 = messageSource.getMessage("description", args, Locale.CHINA);
		String message1 = name1 + " " + msg1;
		String message2 = name2 + " " + msg2;
		logger.info("the msg1 is : " + message1);
		logger.info("the msg2 is : " + message2);
		return message1 + " " + message2;
	}

}
